/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.action.red;

import com.mapping.Red;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value="/")
@Action(value="buscarRed", results={@Result(name="success",location="/GestionRed/gestionRed.jsp")})
public class BuscarRedAction extends ActionSupport{
    private String buscar;
    private List<Red> redes;
    
    @Override
    public String execute(){
        String consulta = "select IDRED,NOMBRERED from red where match (idred,nombrered) against (?1 IN BOOLEAN MODE)";         
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if(buscar== null || buscar.equals("")){
             redes= (List<Red>) em.createNamedQuery("Red.findAll").getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta,Red.class);
            query.setParameter(1,buscar+"*");          
            redes = query.getResultList();
        }
        tx.commit();
        em.close();        
        return SUCCESS;    
    }

    public List<Red> getRedes() {
        return redes;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }        
    
}
